package com.geo.auth.dto;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 登录响应DTO
 */
public class LoginResponse {
    
    /**
     * 访问令牌
     */
    private String accessToken;
    
    /**
     * 刷新令牌
     */
    private String refreshToken;
    
    /**
     * 令牌类型
     */
    private String tokenType = "Bearer";
    
    /**
     * 令牌过期时间（秒）
     */
    private Long expiresIn;
    
    /**
     * 用户信息
     */
    private UserInfo userInfo;
    
    /**
     * 登录时间
     */
    private LocalDateTime loginTime;
    
    /**
     * 登录IP
     */
    private String loginIp;
    
    // 内部类：用户信息
    public static class UserInfo {
        private String userId;
        private String username;
        private String email;
        private String nickname;
        private String avatar;
        private List<String> roles;
        private List<String> permissions;
        
        // Getters and Setters
        public String getUserId() { return userId; }
        public void setUserId(String userId) { this.userId = userId; }
        
        public String getUsername() { return username; }
        public void setUsername(String username) { this.username = username; }
        
        public String getEmail() { return email; }
        public void setEmail(String email) { this.email = email; }
        
        public String getNickname() { return nickname; }
        public void setNickname(String nickname) { this.nickname = nickname; }
        
        public String getAvatar() { return avatar; }
        public void setAvatar(String avatar) { this.avatar = avatar; }
        
        public List<String> getRoles() { return roles; }
        public void setRoles(List<String> roles) { this.roles = roles; }
        
        public List<String> getPermissions() { return permissions; }
        public void setPermissions(List<String> permissions) { this.permissions = permissions; }
    }
    
    // 构造函数
    public LoginResponse() {}
    
    public LoginResponse(String accessToken, UserInfo userInfo, Long expiresIn) {
        this.accessToken = accessToken;
        this.userInfo = userInfo;
        this.expiresIn = expiresIn;
        this.loginTime = LocalDateTime.now();
    }
    
    // Getters and Setters
    public String getAccessToken() {
        return accessToken;
    }
    
    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }
    
    public String getRefreshToken() {
        return refreshToken;
    }
    
    public void setRefreshToken(String refreshToken) {
        this.refreshToken = refreshToken;
    }
    
    public String getTokenType() {
        return tokenType;
    }
    
    public void setTokenType(String tokenType) {
        this.tokenType = tokenType;
    }
    
    public Long getExpiresIn() {
        return expiresIn;
    }
    
    public void setExpiresIn(Long expiresIn) {
        this.expiresIn = expiresIn;
    }
    
    public UserInfo getUserInfo() {
        return userInfo;
    }
    
    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }
    
    public LocalDateTime getLoginTime() {
        return loginTime;
    }
    
    public void setLoginTime(LocalDateTime loginTime) {
        this.loginTime = loginTime;
    }
    
    public String getLoginIp() {
        return loginIp;
    }
    
    public void setLoginIp(String loginIp) {
        this.loginIp = loginIp;
    }
    
    // Builder 模式
    public static Builder builder() {
        return new Builder();
    }
    
    public static class Builder {
        private LoginResponse response;
        
        public Builder() {
            this.response = new LoginResponse();
        }
        
        public Builder accessToken(String accessToken) {
            response.setAccessToken(accessToken);
            return this;
        }
        
        public Builder token(String token) {
            response.setAccessToken(token);
            return this;
        }
        
        public Builder tokenName(String tokenName) {
            response.setTokenType(tokenName);
            return this;
        }
        
        public Builder refreshToken(String refreshToken) {
            response.setRefreshToken(refreshToken);
            return this;
        }
        
        public Builder tokenType(String tokenType) {
            response.setTokenType(tokenType);
            return this;
        }
        
        public Builder expiresIn(Long expiresIn) {
            response.setExpiresIn(expiresIn);
            return this;
        }
        
        public Builder userInfo(UserInfo userInfo) {
            response.setUserInfo(userInfo);
            return this;
        }
        
        public Builder userId(String userId) {
            // 可以创建一个简单的UserInfo或者修改现有的
            if (response.getUserInfo() == null) {
                response.setUserInfo(new UserInfo());
            }
            response.getUserInfo().setUserId(userId);
            return this;
        }
        
        public Builder username(String username) {
            // 可以创建一个简单的UserInfo或者修改现有的
            if (response.getUserInfo() == null) {
                response.setUserInfo(new UserInfo());
            }
            response.getUserInfo().setUsername(username);
            return this;
        }
        
        public Builder nickname(String nickname) {
            // 可以创建一个简单的UserInfo或者修改现有的
            if (response.getUserInfo() == null) {
                response.setUserInfo(new UserInfo());
            }
            response.getUserInfo().setNickname(nickname);
            return this;
        }
        
        public Builder avatar(String avatar) {
            // 可以创建一个简单的UserInfo或者修改现有的
            if (response.getUserInfo() == null) {
                response.setUserInfo(new UserInfo());
            }
            response.getUserInfo().setAvatar(avatar);
            return this;
        }
        
        public Builder userType(String userType) {
            // 用于兼容性，这里就忽略这个字段，因为UserInfo中没有userType字段
            return this;
        }
        
        public Builder roles(List<String> roles) {
            // 可以创建一个简单的UserInfo或者修改现有的
            if (response.getUserInfo() == null) {
                response.setUserInfo(new UserInfo());
            }
            response.getUserInfo().setRoles(roles);
            return this;
        }
        
        public Builder permissions(List<String> permissions) {
            // 可以创建一个简单的UserInfo或者修改现有的
            if (response.getUserInfo() == null) {
                response.setUserInfo(new UserInfo());
            }
            response.getUserInfo().setPermissions(permissions);
            return this;
        }
        
        public Builder loginTime(LocalDateTime loginTime) {
            response.setLoginTime(loginTime);
            return this;
        }
        
        public Builder loginIp(String loginIp) {
            response.setLoginIp(loginIp);
            return this;
        }
        
        public LoginResponse build() {
            return response;
        }
    }
}